package com.tulele.vtool.handler;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.BTeamInfo;
import com.tulele.vtool.model.BTeamRelation;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.combinedEntity.*;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.*;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 团队基础信息表(BTeamInfo)表控制层
 *
 * @author makejava
 * @since 2020-09-09 16:08:52
 */
@Slf4j
@RestController
public class BTeamInfoHandle {

    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 服务对象
     */
    @Resource
    private BTeamInfoService bTeamInfoService;

    /**
     * 服务对象
     */
    @Resource
    private BTeamRelationService bTeamRelationService;

    /**
     * 服务对象
     */
//    @Value("${spring.profiles.active}")
//    private String springProfilesActive;

    @Autowired
    private IUserBrandInfoService userBrandInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param bTeamInfo
     * @return 单条数据
     */
    @PostMapping(value = "/selectOneBTeamInfoById", produces = "application/json;charset=UTF-8")
    public RespData selectOneBTeamInfoById(@RequestHeader("userId") String userId, @RequestBody BTeamInfo bTeamInfo) {

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bTeamInfoService", this.bTeamInfoService.queryById(bTeamInfo.getId()));

        RespData respData = new RespData();
        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询成功");
        return respData;
    }

    /**
     * 测试插入BTeamInfo一个实体
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("insterOneBTeamInfo")
    public BTeamInfo insterOneBTeamInfo() {
        BTeamInfo bTeamInfo = new BTeamInfo();
        bTeamInfo.setTeamName("测试团队名称~");//团队名称
        bTeamInfo.setUserId(1226314);//创建人userId
        bTeamInfo.setGzhCode("test");//公众号标识
        bTeamInfo.setUserId(1);//展示状态（0停用 1正常）
        bTeamInfo.setShareNum(99);//团队分享次数
        bTeamInfo.setCreateTime(DateUtil.getCurDate());//创建时间
        bTeamInfo.setUpdateTime(DateUtil.getCurDate());//更新时间
        bTeamInfo.setTeamRemark("测试团队备注~");//备注

        return this.bTeamInfoService.insert(bTeamInfo);
    }

    /**
     * 创建一个团队，并将自己添加为团队管理员
     *
     * @param
     * @return 单条数据
     */
    @PostMapping(value = "/addOneBTeamInfByUserId", produces = "application/json;charset=UTF-8")
    public RespData addOneBTeamInfByUserId(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamInfo bTeamInfo) {
        RespData respData = new RespData();

        bTeamInfo.setUserId(Integer.valueOf(userId));//创建人userId
        String teamName = bTeamInfo.getTeamName();//团队名称

        if (null == bTeamInfo || null == teamName || teamName.isEmpty() || teamName.length() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队名称不能为空~");
            return respData;
        }

        BTeamInfo bTeamInfoService = this.bTeamInfoService.addOneBTeamInfByUserId(bTeamInfo,springProfilesActive);

        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("bTeamInfoService", bTeamInfoService);

        respData.setData(resultMap);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("创建成功~");
        return respData;
    }

    /**
     * 修改团队名称
     *
     * @param
     * @return 单条数据
     */
    @PostMapping(value = "/updateOneBTeamInfByUserId", produces = "application/json;charset=UTF-8")
    public RespData updateOneBTeamInfByUserId(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamInfo bTeamInfo) {
        RespData respData = new RespData();

        bTeamInfo.setUserId(Integer.valueOf(userId));//创建人userId
        Integer id = bTeamInfo.getId();//团队ID
        String teamName = bTeamInfo.getTeamName();//团队名称

        if (null == bTeamInfo || null == teamName || teamName.isEmpty() || teamName.length() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队名称不能为空~");
            return respData;
        }

        //查询是否是队长
        BTeamInfo BTeamInfoOne = this.bTeamInfoService.queryById(id);

        if (Objects.equals(BTeamInfoOne.getUserId(), Integer.valueOf(userId))) {
            //修改团队表
            BTeamInfoOne.setTeamName(teamName);//团队名称
            this.bTeamInfoService.update(BTeamInfoOne);

            //修改团队成员表
            BTeamRelation bTeamRelation = new BTeamRelation();
            bTeamRelation.setTeamId(bTeamInfo.getId());//团队ID
            bTeamRelation.setTeamName(teamName);//团队名称
            this.bTeamRelationService.updatebTeamRelation(bTeamRelation,springProfilesActive);

            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("修改成功~");
            return respData;
        }

        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("只有队长才能修改团队名称~");
        return respData;
    }

    /**
     * 查询自己所有的团队
     *
     * @param userId
     * @return 多条数据
     */
    @PostMapping(value = "/queryMyBTeamInfoByUserId", produces = "application/json;charset=UTF-8")
    public RespData queryMyBTeamInfoByUserId(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        bTeamRelation.setUserId(Integer.valueOf(userId));//创建人userId
        List<BTeamRelation> bTeamRelationService = this.bTeamRelationService.queryMyBTeamInfoByUserId(bTeamRelation,springProfilesActive);

        if (CollectionUtils.isNotEmpty(bTeamRelationService)) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bTeamRelationService", bTeamRelationService);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询为空~");
        return respData;
    }

    /**
     * 切换团队(@param:teamId)
     *
     * @param userId
     * @return
     */
    @PostMapping(value = "/cutBTeamInfo", produces = "application/json;charset=UTF-8")
    public RespData cutBTeamInfo(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        bTeamRelation.setUserId(Integer.valueOf(userId));//队员userId
        Integer teamId = bTeamRelation.getTeamId();//团队ID

        if (null == bTeamRelation || null == teamId || teamId.intValue() < 100) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队ID为空~");
            return respData;
        }
        int bTeamRelationService = this.bTeamRelationService.cutBTeamInfo(bTeamRelation,springProfilesActive);

        if (bTeamRelationService >= 100) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bTeamRelationService", teamId);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("切换成功~");
            return respData;
        }

        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("切换失败~");
        return respData;
    }

    /**
     * 通过userId查询当前团队成员
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/selectBTeamRelationByTeamId", produces = "application/json;charset=UTF-8")
    public RespData selectBTeamRelationByTeamId(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive) {
        BTeamRelation bTeamRelation = new BTeamRelation();
        RespData respData = new RespData();
        bTeamRelation.setUserId(Integer.valueOf(userId));//队员userId

        BTeamRelation bTeamRelationServiceOne = this.bTeamRelationService.queryBTeamInfoByUserId(bTeamRelation,springProfilesActive);

        if (bTeamRelationServiceOne == null) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("当前没有所在团队~");
            return respData;
        }

        bTeamRelation.setTeamId(bTeamRelationServiceOne.getTeamId());
        List<BTeamInfoUser> bTeamRelationService = this.bTeamRelationService.selectBTeamRelationByTeamId(bTeamRelation,springProfilesActive);

        int teamAllShareCount = new Double(bTeamRelationService.stream().mapToDouble(BTeamInfoUser::getShareCount).sum()).intValue();
        int teamAllLookCount = new Double(bTeamRelationService.stream().mapToDouble(BTeamInfoUser::getLookCount).sum()).intValue();

        if (CollectionUtils.isNotEmpty(bTeamRelationService)) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(5);
            resultMap.put("bTeamRelationServiceOne", bTeamRelationServiceOne);
            resultMap.put("bTeamRelationCount", bTeamRelationService.size());
            resultMap.put("bTeamRelationService", bTeamRelationService);

            resultMap.put("teamAllShareCount", teamAllShareCount);
            resultMap.put("teamAllLookCount", teamAllLookCount);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询为空~");
        return respData;
    }

    /**
     * 通过userId查询当前团队成员
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/selectBTeamRelationByTeamIdAndDay", produces = "application/json;charset=UTF-8")
    public RespData selectBTeamRelationByTeamIdAndDay(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamDayNumber bTeamDayNumber) {
        BTeamRelation bTeamRelation = new BTeamRelation();
        RespData respData = new RespData();
        bTeamRelation.setUserId(Integer.valueOf(userId));//队员userId

        BTeamRelation bTeamRelationServiceOne = this.bTeamRelationService.queryBTeamInfoByUserId(bTeamRelation,springProfilesActive);

        if (bTeamRelationServiceOne == null) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("当前没有所在团队~");
            return respData;
        }

        bTeamRelation.setTeamId(bTeamRelationServiceOne.getTeamId());
        List<BTeamInfoUser> bTeamRelationService = this.bTeamRelationService.selectBTeamRelationByTeamIdAndDay(bTeamRelation, bTeamDayNumber,springProfilesActive);

        if (CollectionUtils.isNotEmpty(bTeamRelationService)) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bTeamRelationService", bTeamRelationService);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询为空~");
        return respData;
    }

    /**
     * 通过团队ID查询当前队长可以移除的团队成员
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/selectRemoveBTeamRelationByTeamId", produces = "application/json;charset=UTF-8")
    public RespData selectRemoveBTeamRelationByTeamId(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        List<BTeamInfoUser> bTeamRelationService = this.bTeamRelationService.selectRemoveBTeamRelationByTeamId(bTeamRelation,springProfilesActive);

        if (CollectionUtils.isNotEmpty(bTeamRelationService)) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bTeamRelationService", bTeamRelationService);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询为空~");
        return respData;
    }

    /**
     * 判断是不是已经在团队
     *
     * @param
     * @return 一条团队成员数据
     */
    @PostMapping(value = "/inviterBTeamRelationBefore", produces = "application/json;charset=UTF-8")
    public RespData inviterBTeamRelationBefore(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        Integer userIds = Integer.valueOf(userId);//加入人userId
        Integer teamId = bTeamRelation.getTeamId();//团队ID
        Integer inviterId = bTeamRelation.getInviterId();//邀请人userId

        if (null == bTeamRelation || null == teamId || teamId.intValue() < 100) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队ID为空~");
            return respData;
        }

        if (null == bTeamRelation || null == inviterId || inviterId.intValue() < 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("邀请人为空~");
            return respData;
        }

        //判断是否存在团队
        BTeamInfo bTeamInfoService = this.bTeamInfoService.queryById(teamId);
        if (null == bTeamInfoService) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队不存在~");
            return respData;
        }

        bTeamRelation.setTeamName(bTeamInfoService.getTeamName());//团队名称


        //判断被邀请人是否已经在团队了,如果在，直接切换到该团队
        bTeamRelation.setUserId(userIds);
        BTeamRelation bTeamRelationServiceUserId = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelation,springProfilesActive);
        if (null != bTeamRelationServiceUserId) {
//            this.bTeamRelationService.cutBTeamInfo(bTeamRelation);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("已经在团队~");
            return respData;
        }

        //判断邀请人是否在团队
        bTeamRelation.setUserId(inviterId);
        BTeamRelation bTeamRelationServiceInviterId = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelation,springProfilesActive);
        if (null == bTeamRelationServiceInviterId) {
//            this.bTeamRelationService.cutBTeamInfo(bTeamRelation);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("邀请人不在团队~");
            return respData;
        }

        //判断是不是自己的团队
        if (Objects.equals(inviterId, Integer.valueOf(userId))) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("自己的团队~");
            return respData;
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("还不在团队~");
        return respData;
    }

    /**
     * 加入团队
     *
     * @param
     * @return 一条团队成员数据
     */
    @PostMapping(value = "/inviterBTeamRelation", produces = "application/json;charset=UTF-8")
    public RespData inviterBTeamRelation(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        Integer userIds = Integer.valueOf(userId);//加入人userId
        Integer teamId = bTeamRelation.getTeamId();//团队ID
        Integer inviterId = bTeamRelation.getInviterId();//邀请人userId

        if (null == bTeamRelation || null == teamId || teamId.intValue() < 100) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队ID为空~");
            return respData;
        }

        if (null == bTeamRelation || null == inviterId || inviterId.intValue() < 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("邀请人为空~");
            return respData;
        }

        //判断是否存在团队
        BTeamInfo bTeamInfoService = this.bTeamInfoService.queryById(teamId);
        if (null == bTeamInfoService) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队不存在~");
            return respData;
        }

        bTeamRelation.setTeamName(bTeamInfoService.getTeamName());//团队名称

        //判断被邀请人是否已经在团队了,如果在，直接切换到该团队
        bTeamRelation.setUserId(userIds);
        BTeamRelation bTeamRelationServiceUserId = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelation,springProfilesActive);
        if (null != bTeamRelationServiceUserId) {
            this.bTeamRelationService.cutBTeamInfo(bTeamRelation,springProfilesActive);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("已经在团队~");
            return respData;
        }

        //判断邀请人是否在团队
        bTeamRelation.setUserId(inviterId);
        BTeamRelation bTeamRelationServiceInviterId = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelation,springProfilesActive);
        if (null == bTeamRelationServiceInviterId) {
            this.bTeamRelationService.cutBTeamInfo(bTeamRelation,springProfilesActive);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("邀请人不在团队~");
            return respData;
        }

        //判断是不是自己的团队
        if (Objects.equals(inviterId, Integer.valueOf(userId))) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("自己的团队~");
            return respData;
        }

        //判断是否已经在团队了,如果不在，添加一条关联再切换到该团队
        bTeamRelation.setUserId(userIds);
        BTeamRelation bTeamRelationServiceOk = this.bTeamRelationService.inviterBTeamRelation(bTeamRelation,springProfilesActive);

        if (bTeamRelationServiceOk != null) {

            /** ljt 20201102 如果没有推荐人，默认关注品牌分类 start */
            try {
                //判断 自己 的 推荐人 是否为空，0 - 为空。
                int refererInDB = Optional.ofNullable(userInfoService.getUserReferer(userIds)).orElse(0);

                if (refererInDB == 0) {
                    //如果没有 推荐人，则设置 邀请人 为 推荐人。
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserId(userIds);
                    userInfo.setReferer(inviterId);
                    userInfoService.modifyWxUserInfo(userInfo);

                    // 判断 自己 有没有关注品牌
                    List<UserBrandCombinedInfo> userBrands = userBrandInfoService.getUserBrandInfo(userIds);

                    // 如果 自己 没有关注品牌，就添加 推荐人 的品牌给自己。
                    if (userBrands == null || userBrands.size() == 0) {

                        List<UserBrandCombinedInfo> referBrands = userBrandInfoService.getUserBrandInfo2(inviterId);

                        if (referBrands != null && referBrands.size() > 0) {
                            for (UserBrandCombinedInfo ref : referBrands) {
                                userBrandInfoService.subBrandInfo(userIds, ref.getBrandId());
                            }
                        }
                    }
                }

            }catch (Exception e) {
                log.error("[WechatHandler]_wxUserLogin_down_error:" + e.getMessage());
            }
            /** ljt 20201102 如果没有推荐人，默认关注品牌分类 end */

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("bTeamRelationService", bTeamRelationServiceOk);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("加入成功~");
            return respData;
        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("加入失败~");
        return respData;
    }

    /**
     * 退出团队
     *
     * @param
     * @return 一条团队成员数据
     */
    @PostMapping(value = "/quitBTeamRelation", produces = "application/json;charset=UTF-8")
    public RespData quitBTeamRelation(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamRelation bTeamRelation) {
        RespData respData = new RespData();

        bTeamRelation.setUserId(Integer.valueOf(userId));//退出人userId
        Integer teamId = bTeamRelation.getTeamId();//团队ID

        if (null == bTeamRelation || null == teamId || teamId.intValue() < 100) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("团队ID为空~");
            return respData;
        }

        //查询是否在团队
        BTeamRelation bTeamRelationService = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelation,springProfilesActive);

        if (bTeamRelationService.getTeamRole() == 2) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("队长不能退出团队~");
            return respData;
        }

        if (bTeamRelationService != null) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            this.bTeamRelationService.deleteById(bTeamRelationService.getId());
            resultMap.put("lastBTeamRelationService", this.bTeamRelationService.addLastBTeamRelation(bTeamRelation,springProfilesActive));

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("退出成功~");
            return respData;
        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("退出失败~");
        return respData;
    }

    /**
     * 队长批量移除团队成员
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/removeBTeamRelation", produces = "application/json;charset=UTF-8")
    public RespData removeBTeamRelation(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamInfoStrUser bTeamInfoStrUser) {
        RespData respData = new RespData();
        String[] userIds = bTeamInfoStrUser.getUserIds().split(",");
        Integer teamId = bTeamInfoStrUser.getTeamId();//团队ID

        if (userIds != null) {
            for (int i = 0; i < userIds.length; i++) {
                BTeamRelation bTeamRelationDto = new BTeamRelation();
                bTeamRelationDto.setTeamId(teamId);
                bTeamRelationDto.setUserId(Integer.valueOf(userIds[i]));
                //查询是否在团队
                BTeamRelation bTeamRelationService = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelationDto,springProfilesActive);

                if (bTeamRelationService != null) {
                    if (bTeamRelationService.getTeamRole() == 2) {
                        respData.setCode(ReturnCode.CLIENT_ERROR);
                        respData.setMsg("队长不能退出团队~");
                        return respData;
                    }

                    this.bTeamRelationService.deleteById(bTeamRelationService.getId());
                    this.bTeamRelationService.addLastBTeamRelation(bTeamRelationDto,springProfilesActive);
                }
            }
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("移除成功~");
            return respData;
        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("未选择~");
        return respData;
    }

    /**
     * 队长批量移除团队成员（@param<List>）
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/removeBTeamRelationList", produces = "application/json;charset=UTF-8")
    public RespData removeBTeamRelationList(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamInfoStrUser bTeamInfoStrUser) {
        RespData respData = new RespData();

        List<BTeamRelation> bTeamRelations = bTeamInfoStrUser.getBTeamRelations();//队员ID
        Integer id = bTeamInfoStrUser.getTeamId();//团队ID

        if (bTeamRelations == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("未选择~");
            return respData;
        }

        //查询是否是队长
        BTeamInfo BTeamInfoOne = this.bTeamInfoService.queryById(id);

        if (Objects.equals(BTeamInfoOne.getUserId(), Integer.valueOf(userId))) {
            for (int i = 0; i < bTeamRelations.size(); i++) {
                BTeamRelation bTeamRelationsOne = bTeamRelations.get(i);

                if (Objects.equals(bTeamRelationsOne.getUserId(), Integer.valueOf(userId))) {
                    respData.setCode(ReturnCode.CLIENT_ERROR);
                    respData.setMsg("队长不能退出团队~");
                    return respData;
                }

                bTeamRelationsOne.setTeamId(BTeamInfoOne.getId());
                //查询是否在团队
                BTeamRelation bTeamRelationService = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelationsOne,springProfilesActive);

                if (bTeamRelationService != null) {
                    this.bTeamRelationService.deleteById(bTeamRelationService.getId());
                    this.bTeamRelationService.addLastBTeamRelation(bTeamRelationsOne,springProfilesActive);
                }
            }
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("移除人数", bTeamRelations.size());

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("移除成功~");
            return respData;

        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("只有队长可以移除团队成员~");
        return respData;
    }

    /**
     * 设置管理员（@param<List>）
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/setManageTeamRelationList", produces = "application/json;charset=UTF-8")
    public RespData setManageTeamRelationList(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody BTeamInfoStrUser bTeamInfoStrUser) {
        RespData respData = new RespData();

        List<BTeamRelation> bTeamRelations = bTeamInfoStrUser.getBTeamRelations();//队员ID
        Integer id = bTeamInfoStrUser.getTeamId();//团队ID

        if (bTeamRelations == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("未选择成员");
            return respData;
        }

        //查询是否是队长
        BTeamInfo BTeamInfoOne = this.bTeamInfoService.queryById(id);

        if (Objects.equals(BTeamInfoOne.getUserId(), Integer.valueOf(userId))) {
            for (int i = 0; i < bTeamRelations.size(); i++) {
                BTeamRelation bTeamRelationsOne = bTeamRelations.get(i);

                if (Objects.equals(bTeamRelationsOne.getUserId(), Integer.valueOf(userId))) {
                    respData.setCode(ReturnCode.CLIENT_ERROR);
                    respData.setMsg("队长不能再设置为管理员~");
                    return respData;
                }

                bTeamRelationsOne.setTeamId(BTeamInfoOne.getId());
                //查询是否在团队
                BTeamRelation bTeamRelationService = this.bTeamRelationService.selectOnBTeamRelation(bTeamRelationsOne,springProfilesActive);

                if (bTeamRelationService != null) {
                    this.bTeamRelationService.setManageById(bTeamRelationService.getId());
                }
            }
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
            resultMap.put("设置人数", bTeamRelations.size());

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("设置成功~");
            return respData;

        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("只有队长可以设置管理员~");
        return respData;
    }

    /**
     * 查询当前团队成员文章
     *
     * @param
     * @return 多条团队成员数据
     */
    @PostMapping(value = "/selectBTeamArticle", produces = "application/json;charset=UTF-8")
    public RespData selectBTeamArticle(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive, @RequestBody GetUserLookedArticleInfoReqBean getUserLookedArticleInfoReqBean) {
        BTeamRelation bTeamRelation = new BTeamRelation();
        RespData respData = new RespData();
        bTeamRelation.setUserId(Integer.valueOf(userId));//队员userId

        BTeamRelation bTeamRelationServiceOne = this.bTeamRelationService.queryBTeamInfoByUserId(bTeamRelation,springProfilesActive);

        if (bTeamRelationServiceOne == null) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("当前没有所在团队~");
            return respData;
        }

        Integer pageNum = getUserLookedArticleInfoReqBean.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = getUserLookedArticleInfoReqBean.getPageSize();
        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = 10;
        }

        PageInfo<BTeamRelationArticle> bTeamRelationArticleServices = this.bTeamRelationService.selectBTeamArticles(pageNum, pageSize, bTeamRelation,springProfilesActive);

        bTeamRelation.setTeamId(bTeamRelationServiceOne.getTeamId());
        List<BTeamInfoUser> bTeamRelationService = this.bTeamRelationService.selectBTeamRelationByTeamId(bTeamRelation,springProfilesActive);

        int teamAllShareCount = new Double(bTeamRelationService.stream().mapToDouble(BTeamInfoUser::getShareCount).sum()).intValue();
        int teamAllLookCount = new Double(bTeamRelationService.stream().mapToDouble(BTeamInfoUser::getLookCount).sum()).intValue();

        if (CollectionUtils.isNotEmpty(bTeamRelationArticleServices.getList())) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(6);

            List<BTeamRelationArticle> collect = bTeamRelationArticleServices.getList();
            List<BTeamRelationArticle> list = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(collect)) {
                list = collect.stream().map(info -> {

                    /* ljt 20210130 热度伪造 start*/
                    info.setArticleHotValue(ToolUtil.setArticleHotValue(info.getArticleHotValue()));
                    /* ljt 20210130 热度伪造 end*/

                    return info;
                }).collect(Collectors.toList());
            }

            resultMap.put("items", list);
            resultMap.put("isIsLastPage", bTeamRelationArticleServices.isIsLastPage());
            resultMap.put("count", bTeamRelationArticleServices.getList().size());
            resultMap.put("team", "team");

            resultMap.put("teamAllShareCount", teamAllShareCount);
            resultMap.put("teamAllLookCount", teamAllLookCount);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("暂无更多~");
        return respData;
    }

    /**
     * 通过团队ID和UserId查询信息
     *
     * @param
     * @return
     */
    @PostMapping(value = "/selectByTeamId", produces = "application/json;charset=UTF-8")
    public RespData selectByTeamId(@RequestHeader("userId") String userId, @RequestBody BTeamInfo bTeamInfo) {
        RespData respData = new RespData();
        Integer oUserId = bTeamInfo.getUserId();

        if(oUserId == null){
            oUserId = Integer.valueOf(userId);
        }

        UserInfo userInfoDo = this.userInfoService.getUserMainInfo(oUserId);

        BTeamInfo bTeamInfoDo = this.bTeamInfoService.queryById(bTeamInfo.getId());

        if (bTeamInfoDo != null) {
            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(3);
            resultMap.put("nickName", userInfoDo.getNickName());
            resultMap.put("avatarUrl", userInfoDo.getAvatarUrl());
            resultMap.put("teamName", bTeamInfoDo.getTeamName());

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功~");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("查询为空~");
        return respData;
    }

}
